$(function () {
    var schoollinkman_tab = $('#schoollinkman_tab');
    var schoollinkman_edit = $("#schoollinkman_edit");
    var schoollinkman_dialog = $("#schoollinkman_dialog");
    var schoollinkman_roles = $("#schoollinkman_roles");
    schoollinkman_tab.datagrid({
        url:'/schoolLinkman/list',
        fit:true,
        fitColumns:true,
        border:false,
        rownumbers:true,
        pagination:true,
        singleSelect:true,
        toolbar:'#schoollinkman_tab_tool',
        columns:[[
            {field:'id',title:'编号',width:50},
            {field:'name',title:'姓名',width:100},
            {field:'gender',title:'性别',width:100,formatter:formatterGender},
            {field:'school',title:'所属学校',width:100,formatter:formatterSchool},
            {field:'schoolCollge',title:'所属院系'},
            {field:'job',title:'职务',width:100},
            {field:'tel',title:'电话',width:100},
            {field:'qq',title:'QQ',width:100},
            {field:'email',title:'Email',width:100},
            {field:'main',title:'是否是主联系人',width:100,formatter:formatterHands},
            {field:'intro',title:'简介',width:100},
        ]]
    });
    //格式化信息
    function formatterSchool(value,row,index) {
        return value ? value.name:"";
    }
    //
    function formatterGender(value,row,index) {
        return value ? value.name:"";
    }

    function formatterHands(value,row,index) {
        return !value ? "<span style='color: red'>否</span>":"<span style='color: green'>是</span>";
    }
    function formatterState(value,row,index) {
        return !value ? "<span style='color: red'>未签约</span>":"<span style='color: green'>已签约</span>";
    }

    var objMethod = {
        //刷新页面
        reloadList:function () {
            schoollinkman_tab.datagrid("reload");
        },
        save:function () {
            var url = "/schoolLinkman/save";
            //获取学校联系人id
            var schoollinkmanId = $("#schoollinkman_id").val();
            if (schoollinkmanId){
                url = "/schoolLinkman/update"
            }
            //提交表单
            schoollinkman_edit.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //关闭窗口
        cancel:function () {
            schoollinkman_dialog.dialog("close");
        },
        //打开编辑窗口
        addschoollinkman:function () {
            //清空表中数据
            schoollinkman_edit.form("clear");
            schoollinkman_dialog.dialog("setTitle","编辑");
            schoollinkman_dialog.dialog("open");
        },
        //设置主联系人
        deleteschoollinkman:function () {
            //获取选中的行
            var selectedRow = schoollinkman_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定删除",function (yes) {
               if (yes){
                   $.post("/schoolLinkman/delete",{id:selectedRow.id,schoolId:selectedRow.school.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //学校联系人编辑
        editschoollinkman:function () {
            //获取选中的行
            var selectedRow = schoollinkman_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            schoollinkman_edit.form("clear");
            console.log(selectedRow);
            //设置关联对象回显---部门回显
            if(selectedRow.school){
                selectedRow['school.id'] = selectedRow.school.id;
            }
            if (selectedRow.genderNum){
                $("#linkman_gender").combobox("setValue",selectedRow.genderNum.id);
            }


            schoollinkman_edit.form("load",selectedRow);
            schoollinkman_dialog.dialog("setTitle","编辑");
            schoollinkman_dialog.dialog("open");
        },

        //高级查询
        queryForList:function () {
            var keyword = $("#keyword").val();
            schoollinkman_tab.datagrid("load",{keyword:keyword});
        },
        downloadList:function () {
            window.location.href="/schoollinkman/download";
        }

    };
    //统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objMethod[cmd]();
    });
    //新增学校联系人弹框
    schoollinkman_dialog.dialog({
        title: '编辑',
        closed: true,
        buttons:'#schoollinkman_edit_tool'
    });
});
